define(
    function (require) {
        require('tpl!./list.tpl.html');

        var util = require('common/util');

        var globalData = require('common/GlobalData').getInstance();

        var showid = 1;
        var page = 1;


        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 20,
                content: function (item) {
                    return showid++;
                }
            },
            {
                title: '电话号码',
                field: 'phone' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.phone;
                }
            },
            {
                title: '年龄',
                field: 'age',
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.age;
                }
            },
            {
                title: '性别',
                field: 'gender' ,
                sortable: true,
                resizable : true,
                width: 70,
                content: function (item) {
                    return psex(item.gender);
                }
            },
            {
                title: '注册时间',
                field: 'addtime' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    return textdate(item.addtime);
                }
            },
            {
                title: '使用次数',
                field: 'num' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.num;
                }
            },
            {
                title: '支付次数',
                field: 'pays' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.pays;
                }
            },
            {
                title: '最后使用时间',
                field: 'etime' ,
                sortable: true,
                resizable : true,
                width: 100,
                content: function (item) {
                    return pytime(item.etime);
                }
            }
            // {
            //     title: '检测机',
            //     field: 'addusername' ,
            //     sortable: true,
            //     resizable : true,
            //     width: 100,
            //     content: function (item) {
            //         return item.addusername;
            //     }
            // }
        ];

        var BaseListView = require('common/ListView');

        function ExperienceListView() {
            BaseListView.apply(this, arguments);
        }

        //时间戳转换
        function  textdate(obj) {
            var date = new Date(obj);
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var minuts = date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes();
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
            return currentdate;
        }
        //性别判断
        function psex(obj){
            if(obj === 0){
                return "男";
            }else if(obj === 1){
                return "女";
            }else{
                return "其他";
            }
        }
        function pytime(obj) {
            if(obj === null){
                return "--  --  --";
            }else {
                var mydate=obj.slice(0,obj.indexOf("."));
              return mydate;
            }
        }

        //检索
        function search() {
            var phone = this.get('mallphone');
            var myDate = new Date();
            var time = textdate(myDate);
            var sbno = this.get('sbno');
            var pay = this.get('pay');
            var sdtime = this.get('sdtime');
            var edtime = this.get('edtime');
            page = 1;
            if(phone.getValue().length>0){
                if(sbno.getValue().length>0 || pay.getValue().length>0 || sdtime.getValue() !== edtime.getValue()){
                    alert("不能与其他选项同时选择！");
                }else{
                    if(phone.getValue().length === 11){
                        this.fire('search', {sbno: sbno.getValue(),pay:pay.getValue(),sdtime: '',edtime: '',phone: phone.getValue()});
                    }else {
                        alert("手机号码必须为11位!");
                    }
                }
            }else {
                if(sdtime.getValue() === time && edtime.getValue() === time){
                    if(sbno.getValue().length>0 || pay.getValue().length>0){
                        this.fire('search', { sbno: sbno.getValue(),pay: pay.getValue(),sdtime: '',edtime: '',phone:phone.getValue()});
                    }else{
                        alert("检索项不能全为空！");
                    }

                }else{
                    this.fire('search', { sbno: sbno.getValue(),pay: pay.getValue(),sdtime: sdtime.getValue(),edtime: edtime.getValue(),phone: phone.getValue() });
                }
            }

        }

        //分页
        function flip(e) {
            var page = e.target.getAttribute('data-page');
            if (/(\+|\-)/.test(page)) {
                page = parseInt(page) + (RegExp.$1 === '+' ? 1 : -1);
            }
            this.fire('flip', { page: page});
        }

        ExperienceListView.prototype.template = 'experienceListPage';

        ExperienceListView.prototype.getUIProperties = function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);

            var str = location.href;
            var num = str.indexOf("=");
            if(num === -1){
                page = 1;
            }else {
                var name = str.substring(num-4, num);
                if(name === 'page'){
                    page = str.substring(num + 1, str.length);
                }else {
                    page = 1;
                }
            }
            showid = (page-1)*10+1;

            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无用户!</p>'
                }
            };
        }


        ExperienceListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);
            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );

            this.get('searchButton').on(
                'click',
                util.bind(search, this)
            );
            $('#list-page').on(
                'click',
                ':not(.disable)' ,
                util.bind(flip, this)
            );
            this.get('list').initChildren();
        }

        require('er/util').inherits(ExperienceListView, BaseListView);
        return ExperienceListView;
    }
);
